// component/menuBar/menuBar.js
import { getCarNumber, decryptGetPhone, bottomNavBar } from '../../config/config'
//获取应用实例
const app = getApp()
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    value: String,
  },
  options: {
    addGlobalClass: true
  },
  /**
   * 组件的初始数据
   */
  data: {
    isShowGetUserInfo:false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    carGoods:0,  //购物车商品数
    // 获取手机号
    getPhoneNumberData:{
      isShowGetPhoneNumber:false
    },
    bottomNavId: '',
    bottomNavData: [], // 底部导航
    show: false
  },
  attached() {
    this.getCarGoodsNumber()
    this.getBottomNavBar()
  },
  /**
   * 组件的方法列表
   */
  methods: {
     // 获取底部导航栏
    getBottomNavBar() {
      app.reqFun.requestForm('GET',bottomNavBar,'',(res)=>{
        if(res.data.success==1){
          if (res.data.data) {
            let value = this.properties.value
            let bottomNavData = res.data.data
            for (let i in bottomNavData) {
              if (bottomNavData[i].linkUrl == value) {
                this.setData({
                  show: true
                })
              }
            }
            this.setData({
              bottomNavData: res.data.data,
              // value: res.data.data[0].linkUrl
            })
          }
          console.log('bottomNavData', this.data.bottomNavData)
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'error',
          });
        }
      })
    },
    getCarGoodsNumber() { //查询购物车商品数-
      var that = this;
      app.reqFun.requestJson('POST', getCarNumber, '', (res) => {
        if (res.data.success == 1) {
          that.setData({
            carGoods: res.data.data
          })
        }
      }, true)
    },
    switchTo(data) {
      let item = data.currentTarget.dataset.data
      console.log('item', item)
      // this.setData({
      //   bottomNavId: item.id
      // })
      let linkType = item.linkType
      let linkAppId =  item.linkAppId
      let linkUrl = item.linkUrl
      if (linkType == 'otherMiniProgram') { // 其他小程序跳转
        this.goMeita(linkAppId)
      } else if (linkType == 'miniPage') {
        wx.reLaunch({
          url: linkUrl
        })
      } else if(linkType == 'h5' && linkUrl!== '/packagBuy/pages/found/crmweb/index') {
        wx.reLaunch({
          url: '/packagBuy/pages/marketing/poster/posterh5/posterh5?h5Url=' + linkUrl
        })
      } else if(linkUrl == '/packagBuy/pages/found/crmweb/index') {
        wx.reLaunch({
          url: '/packagBuy/pages/found/crmweb/index'
        })
      }
      // if (switchTourl == 'shopping' || switchTourl == 'my') {
      //   this.getLogin((re)=>{
      //     if(re){
      //       if(switchTourl == 'shopping') {
      //         wx.reLaunch({
      //           url: '/packagBuy/pages/carbuy/carbuy',
      //         })
      //       } else if(switchTourl == 'my') {
      //         wx.reLaunch({
      //           url: '/packagMy/pages/personal/index/index',
      //         })
      //       }
      //     }
      //   })
      // } else if (switchTourl == 'classify') {
      //   wx.reLaunch({
      //     url: '/pages/classify/classify',
      //   })
      // } else if(switchTourl == 'live') {
      //   wx.reLaunch({
      //     url: '/packageLive/pages/liveRoom/liveRoom'
      //   })
      // } else {
      //   wx.reLaunch({
      //     url: '/pages/index/index',
      //   })
      // }
    },
    // 美搭小程序跳转
    goMeita(linkAppId) {
      wx.navigateToMiniProgram({
        appId: linkAppId,//小程序appid
        path: '',//跳转关联小程序app.json配置里面的地址
        extraData: {
          foo: 'bar'
        },
      //**重点**要打开的小程序版本，有效值 develop（开发版），trial（体验版），release（正式版）
        envVersion: 'release',
        success(res) {
          // 打开成功
        }
      })
    },
    //废弃
    bindGetUserInfo:function(e){  //登录-注册
      var that=this;
      var setFun=setInterval(()=>{
        if(wx.getStorageSync('showGetPhone') && !wx.getStorageSync('loginMsg').userInfo.phone){
          that.setData({
            getPhoneNumberData:{
              isShowGetPhoneNumber:true
            }
          })
          wx.removeStorageSync('showGetPhone')
          clearInterval(setFun)
        }
      },1000)
      app.getUserInfo()
    },
    onCloseShowGetUserInfo(){  //关闭登录弹出层
      this.setData({
        showSetting:false,
        isShowGetUserInfo:false
      })
    },
    /**绑定手机号 ---start*/
    closeGetphone(){
      this.setData({
        getPhoneNumberData:{
          isShowGetPhoneNumber:false
        }
      })
    },
    getPhoneNumber(e){
      let loginMsg = wx.getStorageSync('loginMsg')
      if (e.detail.iv && e.detail.encryptedData) {
        let option = {}
        option.encryptedData = e.detail.encryptedData
        option.iv = e.detail.iv
        option.openid = loginMsg.openid
        option.session_key = loginMsg.session_key
        app.reqFun.requestJson('POST',decryptGetPhone,option,(res)=>{
          if(res.data.success==1){
            loginMsg.bindingPhone = true
            wx.setStorageSync('loginMsg', loginMsg)
            this.setData({
              getPhoneNumberData:{
                isShowGetPhoneNumber:false
              }
            })
            wx.showToast({
              title: '绑定成功。',
            })
          }
        })
      } else {
        console.log('取消')
      }
    },
    /**  绑定手机号 ---end*/
  }
})
